<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/webauthn/helpers.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="utils.js"></script>
<title>Fenced frame content to report the result of navigator.credentials.create</title>

<body>
<script>
function base_path() {
  return location.pathname.replace(/\/[^\/]*$/, '/');
}

standardSetup(function() {
  'use strict';
  async function init() {  // Needed in order to use top-level await.
    const url = new URL(location.href);
    const is_cross_origin_fenced_frame = (url.search == '?cross');

    // This file is meant to be navigated to from a <fencedframe> element. It
    // reports back to the page hosting the <fencedframe> whether or not
    // `navigator.credentials.create` is allowed.
    const credentials_create_key = KEYS['credentials.create'];
    const credentials_create_ack_key = KEYS['credentials.create ACK'];

    // Report whether or not `credentials.create` is allowed.
    if (!is_cross_origin_fenced_frame) {
      createCredential().then(
          () => {
            writeValueToServer(credentials_create_key, 'pass: fenced frame');
          },
          () => {
            writeValueToServer(credentials_create_key, 'fail');
          },
      );
    } else {
      createCredential().then(
          () => {
            writeValueToServer(credentials_create_key, 'unexpected passed');
          },
          () => {
            writeValueToServer(
                credentials_create_key,
                'fail: fenced frame > cross-origin fenced frame');
          },
      );
    }

    // If this page is a cross origin fenced frame in the fenced frame, all we
    // need to do is report the top-level value.
    if (is_cross_origin_fenced_frame)
      return;

    // Wait for ACK, so we know that the outer page has read the last value from
    // the `credentials_create_key` stash and we can write to it again.
    await nextValueFromServer(credentials_create_ack_key);

    const CROSS_ORIGIN_DESTINATION = get_host_info()['HTTPS_REMOTE_ORIGIN'] +
        base_path() + 'create-credential-inner.https.html?cross';
    attachFencedFrame(CROSS_ORIGIN_DESTINATION);
  }

  init();
});
</script>
</body>
